<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\HostOp;
use App\Http\Controllers\Controller;
use App\Model\HostApplyLog;
use App\Model\Message;
use App\Model\User;
use App\Model\UserProfile;
use App\Util\AliyunUtil;
use App\Util\ComUtil;
use App\Util\DateUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class HostApplyController extends Controller
{
    use ModelForm;

    /**
     * 声优列表
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('女用户列表');
            $content->body($this->hostList());
        });
    }

    /**
     * 用户编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface
     * @return Content
     */
    public function create()
    {

        $adminId = Admin::user()->id;
        if($adminId == 48 ){
            return [];
        }
        return Admin::content(function (Content $content) {
            $content->header('添加用户');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * 列表
     * @return Grid
     */
    protected function hostList()
    {
        $grid = new Grid(new UserProfile());
        $where = [
            ['role', UserProfile::ROLE_HOST]
        ];
        $grid->model()->where($where)->orderBy('id', 'desc');
        $grid->user_profile_id('用户资料ID')->sortable();
        $grid->wechat_account('微信号');
        $grid->avatar('头像')->display(function ($data) {
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->nick_name('昵称');
        $grid->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->birthday('年龄')->display(function ($birthday) {
            return DateUtil::newDateDiff($birthday, date('Y-m-d'), 'Y');
        });
        $grid->userInfo()->created_at('注册时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->userInfo()->register_way('注册方式')->display(function ($android_market) {
            return '自主注册';
        });
        $grid->skills('申请开通技能')->display(function ($skills) {
            return str_replace(',', '<br/>', $skills);
        });
        $grid->created_at('申请时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });

        //禁用导出
        $grid->disableExport();
        //禁用新增
        // $grid->disableCreation();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->like('phone', '手机号');
            $filter->equal('platform', '注册平台')->select(User::PLATFORM);
            $filter->equal('userProfile.gender', '性别')->select([0 => '女', 1 => '男', -1 => '其他']);
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * 声优操作
     */
    function apply_op()
    {
        $id = request('id');
        $status = request('status');
        $record = HostApplyLog::where('id', $id)->first();
        if (!$record) return ['ret' => -1, 'message' => "找不到该话单记录！"];

        //更新记录
        $record->update(['status' => $status]);

        //声优角色更新
        if ($status == HostApplyLog::STATUS_OK) {
            $profile = UserProfile::getProfile($record->user_id);
            $upInfo = ['role' => UserProfile::ROLE_HOST];
            //初始化的收费设置
            if ($profile->coin_setting < 1) $upInfo['coin_setting'] = 10;
            if ($profile->video_coin_setting < 1) $upInfo['video_coin_setting'] = 10;
            UserProfile::updateInfo($record->user_id, $upInfo);
            $systemMsg = '恭喜你！你已成功开通个人技能，保持在线可获得更多金币！';
        } elseif ($status == HostApplyLog::STATUS_FAIL) {
            UserProfile::updateInfo($record->user_id, ['role' => UserProfile::ROLE_USER]);
            $systemMsg = '抱歉！你申请开通个人技能的个人资料有误，请重新填写！';
        }
        //发送系统提示
        Message::pushSystem($record->user_id, $systemMsg);
        return ['ret' => 0, 'message' => '操作成功！'];
    }

    /**
     * 用户编辑
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->display('id', '用户内部ID');
            $form->text('userprofile.user_profile_id', '用户ID')->default('');
            $form->text('userprofile.nick_name', '昵称')->default('');
            $form->text('phone', '电话')->default('');
            $form->date('userprofile.birthday', '生日')->default('');
            $form->text('userprofile.city', '城市')->default('');
            $form->display('version', '版本')->default('');
            $form->display('im_account', 'IM账号')->default('');
            $form->select('userprofile.gender', '性别')->options([0 => '女', 1 => '男']);
        });
    }
}
